(function($, Drupal){

Drupal.behaviors.bannerSlick = {
  attach: function(context) {
    var $banner = $('.banner.slick');
    if ($banner.length) {

      // $banner.on('setPosition', function(slick){
      //   // console.log(slick);
      // });

      $banner.slick({
        // fade: true,
        dots: true,
        arrows: false,
        autoplay: true
      });
    }
  }
}

Drupal.behaviors.photosetSlider = {
  attach: function(context) {
    var el = $('.view-albums.view-display-id-block_latest .view-content', context);
    if (el.length) {
      el.slick({
        slidesToShow: 5,
        slidesToScroll: 1,
        autoplay: true,
        autoplaySpeed: 4000,
      });
    }
  }
}

Drupal.behaviors.eventPage = {
  attach: function(context) {

    $video = $('#video', context);
    if ($video.length) {
      var video = '<embed src="http://player.youku.com/player.php/sid/XMTM1OTIxODgxNg==/v.swf" allowFullScreen="true" quality="high" width="970" height="580" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"></embed>';
      $('img', $video).click(function(){
        $(this).after(video).remove();
      });
    }

    $('.share .btn').bind('click', function(){
      $(this).siblings('.media').slideToggle(200);
      return false;
    });

  }
}

Drupal.behaviors.talkComments = {
  attach: function(context) {
    $('.view-comments', context).on('click', function(){
      $('#' + $(this).data('target')).slideToggle(300);
    });
  }
}

Drupal.behaviors.amazeModal = {
  attach: function(context) {
    // $('body').once('am-modal-init').append(Drupal.theme('amazeModal'));

    var modal = $('#amaze-modal');

    $('a.am-modal-load', context).once('am-modal')
    .bind('click tap', function(ev){
      var _self = $(this);

      // $.ajax({
      //  url: _self[0].href,
      //  beforeSend: function(){},
      //  success: function(data){
      //    $('.am-modal-bd', modal).html(data.html);
      //    modal.modal({closeViaDimmer: 0});
      //  },
      //  error: function(xhr, error){}
      // });

      // console.log(_self[0].href);
      

      return false;
    });
  }
}

Drupal.theme.prototype.amazeModal = function () {
  var html = ''
  html += '  <div class="am-modal am-modal-no-btn" tabindex="-1" id="amaze-modal">';
  html += '    <div class="am-modal-dialog">';
  html += '      <div class="am-modal-hd">';
  html += '        <span id="modal-title"></span>';
  html += '        <a href="javascript: void(0)" class="close am-close am-close-spin" data-am-modal-close>&times;</a>';
  html += '      </div>';
  html += '      <div id="modal-content" class="am-modal-bd">';
  html += '      </div>';
  html += '    </div>';
  html += '  </div>';

  return html;
}

/**
 * Overrided rate.js
 */
Drupal.rateVote = function(widget, data, token) {
  // Invoke JavaScript hook.
  widget.trigger('eventBeforeRate', [data]);

  $(".rate-info", widget).text(Drupal.t('Saving vote...'));

  // Random number to prevent caching, see http://drupal.org/node/1042216#comment-4046618
  var random = Math.floor(Math.random() * 99999);

  var q = (Drupal.settings.rate.basePath.match(/\?/) ? '&' : '?') + 'widget_id=' + data.widget_id + '&content_type=' + data.content_type + '&content_id=' + data.content_id + '&widget_mode=' + data.widget_mode + '&token=' + token + '&destination=' + encodeURIComponent(Drupal.settings.rate.destination) + '&r=' + random;
  if (data.value) {
    q = q + '&value=' + data.value;
  }

  // fetch all widgets with this id as class
  widget = $('.' + widget.attr('id'));

  $.get(Drupal.settings.rate.basePath + q, function(response) {
    if (response.match(/^https?\:\/\/[^\/]+\/(.*)$/)) {
      // We got a redirect.
      document.location = response;
    }
    else {
      // get parent object
      var p = widget.parent();

      // Invoke JavaScript hook.
      widget.trigger('eventAfterRate', [data]);

      var r = $(response),
          msg = r.find('.rate-info').text();
      
      if (msg.length) {
        alert(msg);
      }

      r.find('.rate-info').remove();

      widget.before(r);

      // remove widget
      widget.remove();
      widget = undefined;

      Drupal.attachBehaviors(p);
    }
  });

  return false;
}

})(jQuery, Drupal);